package com.itttq.gen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itttq.gen.model.po.ProjectGenHisPo;
import com.itttq.gen.model.qo.ProjectHisQo;
import com.itttq.gen.model.to.IdsTo;
import com.itttq.gen.model.to.ResultTo;
import com.itttq.gen.service.ProjectGenHisService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("projectGenHis")
@Api(tags = "project")
public class ProjectGenHisController extends BaseController {
    @Autowired
    private ProjectGenHisService projectGenHisService;
    /**
     * 保存到数据库。如果传入的对象有ID，则更新对象；没有ID则新增对象
     * @param projectHis 保存的对象
     */
    @PostMapping("/save")
    public void saveProjectHis(@RequestBody @NotNull @Validated ProjectGenHisPo projectHis) {
        if (null == projectHis.getId()) {
            projectGenHisService.addProjectHis(projectHis);
        } else {
            projectGenHisService.updateProjectHis(projectHis);
        }
    }

    /**
     * 根据ID删除记录
     * @param id
     */
    @PostMapping("/del/{id}")
    public void del(@PathVariable("id") long id) {
        projectGenHisService.delProjectHis(id);
    }
    /**
     * 根据ID获取单个对象信息
     * @param id
     */
    @GetMapping("/projectHis/{id}}")
    public ProjectGenHisPo projectHis(@PathVariable("id") long id) {
        return projectGenHisService.getProjectHis(id);
    }

    /**
     * 根据条件查询记录并分页
     * @param projectHisQo 查询对象
     */
    @GetMapping("/projectHiss")
    public IPage<ProjectGenHisPo> projectHiss(ProjectHisQo projectHisQo) {
        return projectGenHisService.getProjectHisAndPage(projectHisQo);
    }

    /**
     * 根据条件查询记录不分页
     * @param projectHisQo 查询对象
     */
    @GetMapping("/projectHisList")
    public List<ProjectGenHisPo> projectHisList(ProjectHisQo projectHisQo) {
        return projectGenHisService.getProjectHiss(projectHisQo);
    }

    /**
     * 根据ID集合批量删除数据
     * @param idsTo ID集合对象
     */
    @PostMapping("/delBatch")
    public void delBatch(@RequestBody IdsTo idsTo) {
        Assert.isTrue(null != idsTo && null != idsTo.getIds(), ResultTo.getCodeAndMsg(ResultTo.ResultCode.DATA_NO, "请选择要删除的记录"));
        projectGenHisService.delProjectHisByIds(idsTo.getIds());
    }
}
